package com.co.vote.web.controller.link;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.co.common.utils.PageReq;
import com.co.server.constant.Constant;
import com.co.server.entity.link.TLink;
import com.co.server.entity.link.TLinkClass;
import com.co.server.entity.link.aide.TLinkQuery;
import com.co.server.service.link.TLinkClassService;
import com.co.server.service.link.TLinkService;
import com.co.vote.constant.HttpStatusCode;
import com.co.vote.utils.SessionUtil;

@Controller
@RequestMapping("/link")
public class LinkController {

	@Resource
	private TLinkService tLinkService;
	@Resource
	private TLinkClassService tLinkClassService;


	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list( Model model,HttpSession session,PageReq pageReq,TLinkQuery query ) {
		pageReq.setSort(new Sort(Direction.DESC,"t.create_time"));
		Page<TLink> page = tLinkService.queryPageList(query, pageReq);
		model.addAttribute("page", page);
		return "/link/list";
	}
	
	@RequestMapping(value = "/myList", method = { RequestMethod.GET, RequestMethod.POST })
	public String myList( Model model,HttpSession session,PageReq pageReq,TLinkQuery query ) {
		Integer userId = SessionUtil.getUserId(session);
		query.setUserId(userId);
		Page<TLink> page = tLinkService.queryPageList(query, pageReq);
		model.addAttribute("page", page);
		return "/link/myList";
	}
	
	@ResponseBody
	@RequestMapping(value = "/ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
	public Map<String, Object> ajaxList( Model model,HttpSession session,PageReq pageReq,TLinkQuery query ) {
		Page<TLink> page = tLinkService.queryPageList(query, pageReq);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		result.put("page", page);
		return result;
	}

	@RequestMapping(value = "/add", method = { RequestMethod.GET,RequestMethod.POST })
	public String addInit( Model model,HttpSession session) {
		List<TLinkClass> linkClassList = tLinkClassService.queryList();
		model.addAttribute("linkClassList", linkClassList);
		return "link/add";
	}

	@RequestMapping(value = "/edit/{id}", method = { RequestMethod.GET,RequestMethod.POST })
	public String edit( Model model,HttpSession session,@PathVariable Integer id) {
		Integer userId = SessionUtil.getUserId(session);
		TLinkQuery tLinkQuery = new TLinkQuery();
		tLinkQuery.setUserId(userId);
		tLinkQuery.setId(id);
		TLink tLink = tLinkService.queryOne(tLinkQuery);
		model.addAttribute("tLink", tLink);
		return "link/edit";
	}

	@RequestMapping(value = "/save", method = { RequestMethod.GET,RequestMethod.POST })
	public String save(Model model,HttpSession session,TLink tLink,PageReq pageReq) {
		if(tLink.getId()==null){
			tLink.setCreateTime(new Date());
			tLink.setStatus(Constant.STATUS_ACTIVE);
			tLink.setUserId(SessionUtil.getUserId(session));
			tLinkService.add(tLink);
		}else{
			tLinkService.updateByIdSelective(tLink);
		}
		return "redirect:/link/list";
	}

	@ResponseBody
	@RequestMapping(value = "/delete/{id}", method = { RequestMethod.GET,RequestMethod.POST })
	public Map<String, Object> delete(@PathVariable Integer id) {
		tLinkService.deleteById(id);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		return result;
	}
}

	
